package com.utf999.qiangmanger.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.utf999.qiangmanger.controller.permission.AuthorityAnnotation;
import com.utf999.qiangmanger.controller.permission.AuthorityScope;
import com.utf999.qiangmanger.controller.permission.PromptModeType;
import com.utf999.qiangmanger.entity.JsonEntity;
import com.utf999.qiangmanger.entity.orm.StorefrontEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.iface.IStorefrontService;

/**
 * @author yinwenjie
 */
@Controller
@RequestMapping(value={"/storefront"})
public class StorefrontController extends BaseController {
	
	private static Log controllerLog4j = LogFactory.getLog(StorefrontController.class);
	
	@Autowired
	private IStorefrontService storefrontService;
	
	/**
	 * 这个方法用于在指定的公司下添加一个新的门店信息
	 * @param request
	 * @param response
	 * @param companyid 公司信息uid
	 * @param storeName 新的门店名称
	 * @param storeadd 可能存在的门店地址
	 * @param storephone  联系电话
	 * @param storeadminname 门店的管理员用户名
	 * @param storeadminpassword 门店的管理员密码信息
	 */
	@RequestMapping(value={"/appendStorefront"} , method=RequestMethod.POST)
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin} , promptMode=PromptModeType.AJAX)
	public void appendStorefront(HttpServletRequest request , HttpServletResponse response , String companyid , String storeName , String storeadd , String storephone , String storeadminname , String storeadminpassword) {
		JsonEntity jsonEntity = new JsonEntity();
		
		StorefrontEntity storefronts = null;
		try {
			storefronts = this.storefrontService.appendStorefront(companyid, storeName, storeadd, storephone, storeadminname, storeadminpassword);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(storefronts);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 这个方法用于修改指定门店的基本信息。<br>
	 * 基本信息只包括：门店名称、门店地址和门店联系电话（其他信息就不能进行修改了）
	 * @param request
	 * @param response
	 * @param storeid 需要进行修改门店编号
	 * @param storeName 新的（也可能没有变化）门店名称信息
	 * @param storephone 新的（也可能没有变化）门店联系电话
	 * @param storeadd 新的（也可能没有变化）门店地址信息
	 */
	@RequestMapping(value={"/updateStorefront"} , method=RequestMethod.POST)
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.StoreAdmin} , promptMode=PromptModeType.AJAX)
	public void updateStorefront(HttpServletRequest request , HttpServletResponse response , String storeid , String storeName , String storephone , String storeadd) {
		JsonEntity jsonEntity = new JsonEntity();
		
		try {
			this.storefrontService.updateStorefront(storeid, storeName, storephone, storeadd);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 超级管理员启用或者禁用指定店面服务的操作。当一个店面没有给服务费的情况下，通常进行这个操作
	 * @param request
	 * @param response
	 * @param storeid 进行操作的 店面id
	 */
	@RequestMapping(value={"/updateStorefrontStatus"} , method=RequestMethod.POST)
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin} , promptMode=PromptModeType.AJAX)
	public void updateStorefrontStatus(HttpServletRequest request , HttpServletResponse response , String storeid) {
		JsonEntity jsonEntity = new JsonEntity();
		try {
			StorefrontEntity store = (StorefrontEntity)this.storefrontService.getEntity(storeid);
			if(store.getStatus() == 1) {
				store.setStatus(0);
			} else {
				store.setStatus(1);
			}
			this.storefrontService.update(store);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(null);
			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (Exception e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 查询指定的公司下，可用的商铺信息
	 * @param request
	 * @param response
	 * @param companyid
	 */
	@RequestMapping(value={"/queryStorefrontList"} , method=RequestMethod.POST)
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin} , promptMode=PromptModeType.AJAX)
	public void queryStorefrontList(HttpServletRequest request , HttpServletResponse response , String companyid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		List<StorefrontEntity> storefronts = null;
		try {
			//查询全部的店面，无论店面状态如何
			storefronts = this.storefrontService.queryStorefrontList(companyid , null);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(storefronts);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "admins"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
	
	/**
	 * 查询指定的店面信息（不包括其公司信息）
	 * @param request
	 * @param response
	 * @param storeid 需要查询的店面id
	 */
	@RequestMapping(value={"/queryStorefront"} , method=RequestMethod.POST)
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin} , promptMode=PromptModeType.AJAX)
	public void queryStorefront(HttpServletRequest request , HttpServletResponse response , String storeid) {
		JsonEntity jsonEntity = new JsonEntity();
		
		StorefrontEntity storefront = null;
		try {
			storefront = (StorefrontEntity)this.storefrontService.getEntity(storeid);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(storefront);
			this.writeResponseMsg(response, jsonEntity , new String[]{"company" , "admins"});
		} catch (Exception e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(ResponseCode.E501);
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		} 
	}
}
